let $ = jQuery;
let reqUrl;
$(function () {

    $('#exampleTableEvents').bootstrapTable({
        method:"post",
        url: "/receipts/pageReceiptsByStock",
        contentType: "application/x-www-form-urlencoded",
        dataType:"json",
        striped: true, //是否显示行间隔色
        pageNumber: 1, //初始化加载第一页，默认第一页
        pagination: true,//是否分页
        sidePagination: 'server',//在服务器分页
        queryParamsType:'limit',
        queryParams:queryParams,
        pageSize: 10,//单页记录数
        checkbox:true,
        singleSelect:true,
        clickToSelect: true,//是否启用点击选中行
        showToggle: false,
        showColumns: false,
        columns: [
            {
                title: 'ID',
                field: 'rid',
                align: 'center',
                visible: false
            },
            {
                title: '单据编号',
                field: 'rno',
                align: 'center',
                sortable: false
            },
            {
                title: '入库时间',
                field: 'createTime',
                formatter:function (index,row){
                    return formatDate(row.createTime);
                },
                align: 'center',
            },
            {
                title: '操作人',
                align: 'center',
                field: 'operator'
            },
            {
                title: '审核人',
                field: 'auditor',
                align: 'center',
                formatter:function (value, row, index){
                    if(row.auditor==="" || row.auditor===null){
                        return "该商品未审核"
                    }else {
                        return row.auditor
                    }
                }
            }
        ],
        locale: 'zh-CN',//中文支持,
    });
    //点击搜索
    $('#search').click(function () {
        $('#exampleTableEvents').bootstrapTable('refresh', {url: '/receipts/pageReceiptsByStock'});//url为后台action
    })

    $('#exampleTableEvents').on("click-row.bs.table",function (row,el,field) {
        $.ajax("receiptsDetails/findReceiptsDetailsByRid", {
            method: "post",
            headers: {
                contentType: "application/x-www-form-urlencoded",
            },
            data: {rid:el.rid},
            success: (res) => {
                //渲染
                let list = res.data;
                //清除box中除去头部的所有标签
                $("#box>div:gt(0)").remove();
                //遍历list中的数据
                for (let data of list) {
                    let str = "<div class='row'>" +
                                    "<div class='col-xs-6'>" +
                                        "<input type='text' name='gid' hidden value="+data.gid+" />" +
                                        "<input type='text' name='gname' disabled class='form-control text-center' value="+data.name+" /> " +
                                    "</div>" +
                                    "<div class='col-xs-2'>" +
                                        "<input type='text' name='number' disabled class='form-control text-center' value="+data.number+">" +
                                    "</div> " +
                                    "<div class='col-xs-4'><div class='input-group m-b'>" +
                                        "<input type='text' name='produced_date' disabled class='form-control text-center' value="+formatDate(data.updateTime)+">" +
                                    "</div>" +
                                "</div>";
                    $("#box").append(str);
                }

                $("#rid").val(el.rid);

            }
        })
    })
    $('#btn-submit').on("click",function (){
        reqUrl="/stock/addStock";
        let gid = $("#gid").val();
        let name = $("#name").val();

        let price = $("#price").val();
        let details = $("#details").val();
        let gcid = $("#gcid").val();

        let img = $("#img").val();
        let specification = $("#specification").val();
        let manufacturer = $("#manufacturer").val();

        let SellingPrice = $("#SellingPrice").val();
        let StorageQuantity = $("#StorageQuantity").val();

        let productDate = $("#productDate").val();

        var data = new FormData()
        data.append("gid",gid)
        data.append("name",name)

        data.append("price",price)
        data.append("details",details)
        data.append("gcid",gcid)

        data.append("img",img)
        data.append("manufacturer",manufacturer)
        data.append("specification",specification)

        data.append("SellingPrice",SellingPrice)
        data.append("StorageQuantity",StorageQuantity)
        data.append("productDate",productDate)

        req(reqUrl,data);

    })
    /*图片异步上传*/
    $("#pic").change(function () {
        //上传的请求
        let formDateObj = new FormData();
        //添加文件对象
        formDateObj.append('file',$('#pic')[0].files[0])
        //4.将对象基于ajax发送给后端
        $.ajax({
            url:'/uploadForAjax',
            type:'post',
            data:formDateObj,  //直接将对象放在data后面
            //ajax发送文件必须要指定两个参数
            contentType:false,  //不要使用任何编码，django后端能够自动识别formdata对象
            processData:false,  //告诉浏览器不要对你的数据进行任何处理
            success:function (data) {
                //回填数据到隐藏域
                $("#img").val(data.data.url);
                let img = '<img src="'+data.data.url+'" style="min-width: 80%;max-width:100%;position: absolute;padding-right: 20px" alt="">';
                $("#goodsImg").after(img);
            }
        })
    });
})


//请求服务数据时所传查询参数
function queryParams(params){
    return{
        pageSize: params.limit,
        pageNum:parseInt(params.offset/params.limit)+1,
        typeid:$("#typeid").val(),
        status:$('#status').val(),
        rno:$('#rno').val(),
        startTime:$('#startDate').val(),
        endTime:$('#endDate').val()
    }
}
function req(url,data) {
    $.ajax(url,{
        method:"post",
        headers:{
            contentType: "application/x-www-form-urlencoded",
        },
        contentType : false,
        processData : false,
        data:data,
        success:(res)=>{
            console.log(res)
            if(res.status==="success"){
                swal({title: "入库成功"},function (){
                    window.location.reload()
                })
            }else{
                swal({title: res.msg},function (){
                    window.location.reload()
                });
            }
            setTimeout(function () {
                swal.close()
                window.location.reload()
            },3000)
            //清除表单数据
            document.getElementById("myform").reset();
            //隐藏模板
            $("#myModal5").modal("hide");
            //刷新表格
            $('#exampleTableEvents').bootstrapTable('refresh', {url: '/role/listAll'});
        }
    })

}

